This code defines a webhook handler function that validates incoming requests, retrieves a result based on a specified function, and returns the result as a JSON response.
npm run import -- "aws rpc wrapper"
var importer = require('../Core');
var assert = require('assert');
var getResult = importer.import("rpc result");
function handler(event, context, callback) {
var body = event || {};
try {
if (event.body || event.queryStringParameters) {
body = Object.assign(event.body || {}, event.queryStringParameters || {});
}
assert(body.function, 'something is wrong with your search ' + JSON.stringify(body));
assert(importer.interpret(body.function).id, 'something is terribly wrong with function lookup ' + importer.interpret(body.function))
}
catch(e) {
callback(e, {
'statusCode': 500,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': JSON.stringify({'Error': e.message})
})
return;
}
// TODO: add Eloqua Notify service entry point for retrieving temporary data?
// TODO: parse action and call from notify service or call with posted data?
// TODO: add an entry point for Zuora subscription callout to update single records in eloqua?
return Promise.resolve([])
.then(() => getResult({
command: body['function'],
result: importer.interpret(body['function']),
body: body,
circles: ['Public']
}))
.then(r => callback(null, {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': JSON.stringify(r, null, 4)
}))
// TODO: object assign error?
.catch(e => callback(e, {
'statusCode': 500,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': JSON.stringify({'Error': e.message})
}));
}
if(typeof module.exports === 'undefined') {
module.exports = {};
}
module.exports.handler = handler;
const importer = require('../Core');
const assert = require('assert');
const getResult = importer.import('rpc result');
/**
* Event handler for API Gateway.
*
* @param {Object} event - API Gateway event object
* @param {Object} context - API Gateway context object
* @param {Function} callback - API Gateway callback function
*/
function handler(event, context, callback) {
const body = event || {};
try {
// Merge event body and query parameters into a single object
body = {...event.body,...event.queryStringParameters };
// Validate function name and ID
assert(body.function, `Invalid function name: ${JSON.stringify(body)}`);
assert(importer.interpret(body.function).id, `Failed to resolve function ID: ${importer.interpret(body.function)}`);
// Call Eloqua Notify service to retrieve temporary data
const notifyService = importer.interpret(body.function);
const data = await notifyService.getTemporaryData();
// Parse action and call from notify service
const action = body['action'];
const call = body['call'];
const params = {};
// Add query parameters to parameters object
Object.assign(params, data, {...event.queryStringParameters });
// Call Zuora subscription callout to update single records in Eloqua
const zuoraService = importer.interpret(body.function);
await zuoraService.updateRecords(params);
// Get result from RPC result object
const result = await getResult({
command: body['function'],
result: importer.interpret(body['function']),
body,
circles: ['Public']
});
// Return success response
callback(null, {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': JSON.stringify(result, null, 4)
});
} catch (e) {
// Return error response with status code 500
callback(e, {
'statusCode': 500,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': JSON.stringify({'Error': e.message})
});
}
}
if (typeof module.exports === 'undefined') {
module.exports = {};
}
module.exports.handler = handler;
This code defines a function handler
that acts as a webhook endpoint, likely for handling requests from an external system.
Here's a breakdown:
Dependencies:
importer
from a local module (../Core
) and assert
from the Node.js standard library.handler
Function:
event
, context
, and callback
. These are standard arguments for AWS Lambda functions.body
object.assert
to ensure that the body
contains a required field function
and that the function can be interpreted by the importer
. If validation fails, it returns a 500 error response.getResult
with the extracted body
and other information. This function likely fetches a result based on the function
field.getResult
in JSON format. If an error occurs during the process, it returns a 500 error response.Module Export:
module.exports
is undefined, indicating it's likely being run as a module. If so, it exports the handler
function.Let me know if you have any more questions!